#include "RawPackageParser.h"

RawPackageParser::RawPackageParser(): 
	state_(package_size_1),package_size_(0)
{
}

void RawPackageParser::reset()
{
	state_ = package_size_1;
	package_size_ = 0;
}

bool RawPackageParser::consume(RawPackage& package, const char input)
{
	LogFunctionEnter;

	if (package_size_1 == state_)
	{
		package_size_ = input;

		state_ = package_size_2;

		return false;
	}
	else if (package_size_2 == state_)
	{
		package_size_ |= uint16(input) << 8;
		package.setExpectSize(package_size_);
		state_ = package_buffer;
	}
	else if (package_buffer == state_)
	{
		package.push(input);
		if (package_size_ == package.getDataSize() + SIZE_LENGTH)
		{
			reset();
			return true;
		}

		return false;
	}

	return false;

}